package com.f5.management.service.impl;

import com.f5.management.dao.EquipmentDAO;
import com.f5.management.entity.Equipment;
import com.f5.management.service.EquipmentService;
import com.f5.management.utils.DeviceUtil;
import com.f5.management.utils.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class EquipmentServiceImpl implements EquipmentService {
    @Autowired
    private EquipmentDAO equipmentDAO;


    @Override
    public boolean add(Equipment equipment) {
        //自动生成编号
        equipment.setDeviceNum(DeviceUtil.createEquipmentDevice());
        return equipmentDAO.insert(equipment) > 0 ? true : false;
    }


    @Override
    public PageResult<List<Equipment>> findByPage(int page, int size) {
        PageResult<List<Equipment>> result = new PageResult<List<Equipment>>();
        Page<Equipment> startPage = PageHelper.startPage(page, size);
        List<Equipment> equipments = equipmentDAO.findByPage();
        result.setCurrentPage(page);
        result.setSize(size);
        result.setTotal(startPage.getTotal());
        result.setTotalPage(startPage.getPages());
        result.setData(equipments);

        System.out.println(equipments);
        return result;
    }

    @Override
    public Integer findCidByName(String communityName) {
        return equipmentDAO.findCidByName(communityName);
    }

    @Override
    public Equipment findById(int id) {

       return equipmentDAO.findById(id);
    }

    @Override
    public boolean updateInfo(Equipment equipment) {
        return equipmentDAO.updateInfo(equipment) > 0 ? true : false;
    }

    @Override
    public boolean delInfo(int id) {
        return equipmentDAO.delInfo(id) > 0 ? true : false;
    }
}
